package com.qdxwx.manage.controller.manager;

import com.qdxwx.common.entity.Ztree;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.menu.MenuService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Menu;
import com.qdxwx.models.manager.Role;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜单信息
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/menu")
public class ManagerMenuController extends BaseController
{
    private String prefix = "menu";

    @Autowired
    private MenuService menuService;

    @GetMapping(value = "/list.html")
    @Permissions("menu:list")
    public String menu()
    {
        return prefix + "/menu-list";
    }

    @Permissions("menu:list")
    @PostMapping("/list.json")
    @ResponseBody
    public List<Menu> list(Menu menu)
    {
        Integer managerId = SessionUtils.getManagerId();
        return menuService.getList(menu, managerId);
    }

    /**
     * 删除菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @Permissions("menu:remove")
    @GetMapping("/delete.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        Integer menuId = TKConvert.toInteger(request.getParameter("id"));
        if (menuService.countByParentId(menuId) > 0) {
            return AjaxResult.warn("存在子菜单,不允许删除");
        }
        menuService.deleteRoleMenuById(menuId);
        //if (menuService.countRoleMenuByMenuId(menuId) > 0)
        //{
        //    return AjaxResult.warn("菜单已分配,不允许删除");
        //}
        return toAjax(menuService.deleteById(menuId));
    }

    /**
     * 新增
     */
    @GetMapping("/add.html")
    @Permissions("menu:edit")
    public String add(HttpServletRequest request, ModelMap mmap)
    {
        int parentId = TKConvert.toInteger(request.getParameter("parentId"));
        Menu menu = null;
        if (0 != parentId)
        {
            menu = menuService.getById(parentId);
        }
        else
        {
            menu = new Menu();
            menu.setMenuId(0);
            menu.setMenuName("主目录");
        }
        mmap.put("menu", menu);
        return prefix + "/menu-add";
    }

    /**
     * 新增保存菜单
     */
    @Log(title = "添加菜单", businessType = BusinessType.INSERT)
    @Permissions("menu:edit")
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(@Validated Menu menu)
    {
        if (menuService.isExistMenuName(menu))
        {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateBy(SessionUtils.getLoginName());
        return toAjax(menuService.insert(menu));
    }

    /**
     * 修改菜单
     */
    @GetMapping("/edit.html")
    @Permissions("menu:edit")
    public String edit(HttpServletRequest request, ModelMap mmap)
    {
       int menuId = TKConvert.toInteger(request.getParameter("id"));
        mmap.put("menu", menuService.getById(menuId));
        return prefix + "/menu-edit";
    }

    /**
     * 修改保存菜单
     */
    @Log(title = "编辑菜单", businessType = BusinessType.UPDATE)
    @Permissions("menu:edit")
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(@Validated Menu menu)
    {
        if (menuService.isExistMenuName(menu))
        {
            return error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setUpdateBy(SessionUtils.getLoginName());
        return toAjax(menuService.update(menu));
    }

    /**
     * 选择菜单图标
     */
    @GetMapping("/icon.html")
    public String icon()
    {
        return prefix + "/menu-icon";
    }

    /**
     * 校验菜单名称
     */
    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public String checkMenuNameUnique(Menu menu) {
        return menuService.isExistMenuName(menu) ? "1" : "0";
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(Role role)
    {
        Integer managerId = SessionUtils.getManagerId();
        return menuService.getTreeByRole(role, managerId);
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData()
    {
        Integer managerId = SessionUtils.getManagerId();
        return menuService.getTreeByManagerId(managerId);
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Integer menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.getById(menuId));
        return prefix + "/menu-tree";
    }
}